import json

from ws_server import send_message


class RedirectType:
    #  初始化变量
    click_enable = False
    update_enable = False
    online_enable = False
    watched_enable = False
    like_info = {
        'uname': None,
        'like_text': None,
        'click_count': None
    }
    online = {
        'count': None,
        'total_count': None
    }

    #  type1:弹幕消息
    @classmethod
    def DANMU_MSG(cls, jd):
        # 弹幕消息
        if jd['cmd'] == 'DANMU_MSG':
            if jd['info'] is not None:
                data = jd['info']
                Id = data[2][0]
                Nickname = data[2][1]
                Content = data[1]
                #  粉丝勋章级别
                if len(data[3]) > 0:
                    level = data[3][0]
                    club_name = data[3][1]
                else:
                    level = 0
                    club_name = None
                new_message = {
                    'User': {
                        'Id': Id,
                        'Nickname': Nickname,
                        'Level': level,
                        "FansClub": {
                            "ClubName": club_name,
                            "Level": level
                        }
                    },
                    'Content': Content
                }
                new_message = {
                    'Type': 1,
                    'Data': str(new_message).replace("'", r'"')
                }
                new_message = json.dumps(new_message, ensure_ascii=False)
                send_message(new_message)
            else:
                #  其他类型的弹幕（关注主播）
                print(jd)

    #  type2:直播间点赞量
    @classmethod
    def LIKE_INFO_V3(cls, jd):
        if jd['cmd'] == 'LIKE_INFO_V3_CLICK' and cls.click_enable is False:
            cls.like_info['uname'] = jd['data']['uname']
            cls.like_info['like_text'] = jd['data']['like_text']
            cls.click_enable = True
        if jd['cmd'] == 'LIKE_INFO_V3_UPDATE' and cls.update_enable is False:
            cls.update_enable = True
            cls.like_info['click_count'] = jd['data']['click_count']
        if cls.click_enable and cls.update_enable:
            new_message = {
                "Total": cls.like_info['click_count'],
                'User': {
                    'Nickname': cls.like_info['uname']
                },
                'Content': str(cls.like_info['uname']) + " " + str(cls.like_info['like_text']) + ",总点赞" + str(
                    cls.like_info['click_count'])
            }
            new_message = {
                'Type': 2,
                'Data': str(new_message).replace("'", r'"')
            }
            new_message = json.dumps(new_message, ensure_ascii=False)
            send_message(new_message)
            cls.click_enable = False
            cls.update_enable = False
            cls.like_info = {
                'uname': None,
                'like_text': None,
                'click_count': None
            }

    #  type3：进入直播间
    @classmethod
    def INTERACT_WORD(cls, jd):
        uname = jd['data']['uname']
        roomid = jd['data']['roomid']
        uid = jd['data']['uid']
        level = jd['data']['fans_medal']['medal_level']
        club_name = jd['data']['fans_medal']['medal_name']
        new_message = {
            'User': {
                'Id': uid,
                'Nickname': uname,
                "Level": level,
                "FansClub": {
                    "ClubName": club_name,
                    "Level": level
                }
            },
            'Content': uname + ' 来了 直播间',
            'RoomId': roomid
        }
        new_message = {
            'Type': 3,
            'Data': str(new_message).replace("'", r'"')
        }
        new_message = json.dumps(new_message, ensure_ascii=False)
        send_message(new_message)

    #  type5: 送礼物
    @classmethod
    def SEND_GIFT(cls, jd):
        data = jd['data']
        uname = data['uname']
        uid = data['uid']
        gift_id = data['giftId']
        gift_name = data['giftName']
        gift_num = data['num']
        level = jd['data']['medal_info']['medal_level']
        club_name = jd['data']['medal_info']['medal_name']
        new_message = {
            "GiftId": gift_id,
            "GiftName": gift_name,
            "GiftCount": gift_num,
            'User': {
                'Id': uid,
                'Nickname': uname,
                "FansClub": {
                    "ClubName": club_name,
                    "Level": level
                }
            },
            'Content': uname + ":送给主播 " + str(gift_num) + "个" + gift_name
        }
        new_message = {
            'Type': 5,
            'Data': str(new_message).replace("'", r'"')
        }
        new_message = json.dumps(new_message, ensure_ascii=False)
        send_message(new_message)

    #  type6，直播间在线人数
    @classmethod
    def ONLINE_RANK_COUNT(cls, jd):
        if jd['cmd'] == 'ONLINE_RANK_COUNT' and cls.online_enable is False:
            cls.online['count'] = jd['data']['count']
            cls.online_enable = True
        if jd['cmd'] == 'WATCHED_CHANGE' and cls.watched_enable is False:
            # jd = {'cmd': 'WATCHED_CHANGE', 'data': {'num': 14641, 'text_small': '1.4万', 'text_large': '1.4万人看过'}}
            cls.online['total_count'] = jd['data']['text_small']
            cls.watched_enable = True
        if cls.watched_enable is True and cls.online_enable is True:
            new_message = {
                "OnlineUserCount": cls.online['count'],
                "TotalUserCount": cls.online['total_count'],
                "TotalUserCountStr": cls.online['total_count'],
                "OnlineUserCountStr": cls.online['count'],
                "Content": f"当前直播间人数 {cls.online['count']}，累计直播间人数 {cls.online['total_count']}"
            }
            new_message = {
                'Type': 6,
                'Data': str(new_message).replace("'", r'"')
            }
            new_message = json.dumps(new_message, ensure_ascii=False)
            send_message(new_message)
            cls.watched_enable = False
            cls.online_enable = False
            cls.online = {
                'count': None,
                'total_count': None
            }
